package com.token.sysflow.action;

import com.token.base.AgentAction;
import com.token.base.FuncUtil;
import com.token.base.Pl;
import com.token.sql.IntegrateSql;
import com.token.sql.TableBeanPrepareSql;
import com.token.sysflow.bean.FSysApproveProcess;
import com.token.sysflow.bean.FlowFlowInfo;


/**
 * @author chenjianxiong 2020/5/7
 */
public class DeleteFlow implements AgentAction {
    public void executeAction(FuncUtil f) throws Exception {
    }

    public void executeAction(FuncUtil f, IntegrateSql iSql) throws Exception {

        try{
            iSql.setAutoCommit(false);
            FlowFlowInfo flowFlowInfo = new FlowFlowInfo(iSql,f.getUrlQueryString("flowid",true,"流程Id"));
            TableBeanPrepareSql tSql = new TableBeanPrepareSql(flowFlowInfo);
            if(flowFlowInfo.isInitRs()){
                FSysApproveProcess approveProcess = new FSysApproveProcess(iSql);
                if(iSql.selectCount(approveProcess.getTableName(),"FlowId",flowFlowInfo.getFlowId()) == 0) {
                    tSql.setDeletePrepare("flowId = ? and SysId=?");
                    tSql.setPstValue(1, flowFlowInfo.getFlowId());
                    tSql.setPstValue(2, Pl.getSysId());
                    tSql.executeUpdate();
                }else{
                    throw new Exception("该流程还有单据正在审批，不能删除！");
                }
                tSql.close();
                flowFlowInfo.close();
            }else{
                throw new Exception("流程不存在，无法删除！");
            }
            iSql.commit();
        }catch(Exception e){
            iSql.rollback();
            throw e;
        }

    }
}
